package com.foreknow.Servlet;

import com.foreknow.entity.User;
import com.foreknow.service.UserService;
import com.foreknow.service.impl.UserServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "UserServlet1",urlPatterns = "/login")
public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        //如何获取到请求的参数 email=tom&password=123456
        String username =request.getParameter("username");
        String password = request.getParameter("password");
        //调用Service
        UserService service =new UserServiceImpl();
        User user= service.welcome(username,password);
        Gson gson =new Gson();
       String json= gson.toJson(user);
        if (user!=null){
            out.println("登陆成功");
            out.println(json);
        }else{
            out.println("登陆失败");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doPost(request,response);
    }
}
